﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AMEE.BeinControl;
using AMEE.MVVMBase;

namespace AMEE.Demo1WPF.ViewModel
{
    /// <summary>
    /// Bein Data ViewModel to show in WPF.
    /// </summary>
    public class BeinViewModel : ViewModelBase<Bein>
    {
        #region Fields
        #endregion

        #region Properties

        string _state = "unknown";
        /// <summary>
        /// Gets or sets the state of the bein.
        /// </summary>
        /// <value>
        /// The state of the bein.
        /// </value>
        public String BeinState {
            get { return _state; }
            protected set {
                _state = value;
                OnPropertyChanged("BeinState");
            }
        }

        string _id = "noID";
        /// <summary>
        /// Gets or sets the bein unique identifier.
        /// </summary>
        /// <value>
        /// The bein unique identifier. Value = "noID", if parameter is Null.
        /// </value>
        public String BeinID {
            get { return _id; }
            set {
                _id = (value == null) ? "noID" : value;
                OnPropertyChanged("BeinID");
            }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="BeinViewModel" /> class.
        /// </summary>
        /// <param name="modelInstance">The model instance.</param>
        /// <param name="id">The Leg number</param>
        /// <exception cref="System.ArgumentNullException">modelInstance</exception>
        public BeinViewModel(Bein modelInstance, String id)
            : base(modelInstance) {
                if (null == modelInstance)
                    throw new ArgumentNullException("modelInstance");
                modelInstance.StateChanged += onBeinStateChanged;
                this.BeinState = modelInstance.State.ToString();
                this.BeinID = id;
        }
        

        #region private Helpers

        void onBeinStateChanged(object sender, EventArgs e) {
            var sndr = sender as Bein;
            if (null == sndr)
                return;
            if (!sndr.Equals(base.Model))
                return;
            string newState = base.Model.State.ToString();
            if (!newState.Equals(this.BeinState)) {
                this.BeinState = newState;
                //Logger.Info("Bein " + this.BeinID + ", state changed to " + newState);
            }
            else {
                //Logger.Warn("Bein " + this.BeinID + ", state changed event but with unchanged state: " + newState);
            }
        }

        #endregion

    }
}
